<?php

namespace app\admin\controller\roomcardcode;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;
use app\admin\model\VAnnalGame;

/**
 * @icon fa fa-circle-o
 */
class Admindiamond extends Backend
{   
    
    protected $model = null;

    public function _initialize()
    { 
        parent::_initialize();
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        //进行获取信息
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {   
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            
            //总数
            $total = Db::view('y_agencyrechargelog','*')
                    ->view('y_agency','LevelID,AgeType','y_agency.ID=y_agencyrechargelog.AgencyID','LEFT')
                    ->where($where)
                    ->where(['AgeType'=>0])
                    ->order('y_agencyrechargelog.ID desc')
                    ->count();

            $list = Db::view('y_agencyrechargelog','*')
                    ->view('y_agency','LevelID,AgeType','y_agency.ID=y_agencyrechargelog.AgencyID','LEFT')
                    ->where($where)
                    ->where(['AgeType'=>0])
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();  
             
            //处理数据
            $uid_arr = $user_arr = array();
            foreach ($list as $val){
                $uid_arr[] = $val['ReceiverID'];//接收游戏ID
            }
            $user_list = Db::table('fa_y_agency')->where(['uid'=>array('in',$uid_arr)])->select();//接收列表
            foreach ($user_list as $val){
                $user_arr[$val['uid']] = $val['LevelID'];
            }
            foreach($list as $key=>$val){
                if($val['LevelID'] == 0){
                    $list[$key]['LevelID'] = '超级';
                }elseif($val['LevelID'] == 1){
                    $list[$key]['LevelID'] = '一级代理';
                }elseif($val['LevelID'] == 2){
                    $list[$key]['LevelID'] = '二级代理';
                }elseif($val['LevelID'] == 3){
                    $list[$key]['LevelID'] = '三级代理';
                }
                if(!isset($user_arr[$val['ReceiverID']])){
                    $list[$key]['ReceiverLevel'] = '玩家';
                }elseif($user_arr[$val['ReceiverID']] == 1){
                    $list[$key]['ReceiverLevel'] = '一级代理';
                }elseif($user_arr[$val['ReceiverID']] == 2){
                    $list[$key]['ReceiverLevel'] = '二级代理';
                }elseif ($user_arr[$val['ReceiverID']] == 3){
                    $list[$key]['ReceiverLevel'] = '三级代理';
                }
                if($val['ReceiverType'] == 0){
                    $list[$key]['ReceiverType'] = '扣除';
                }elseif($val['ReceiverType'] == 1){
                    $list[$key]['ReceiverType'] = '赠送';
                }elseif($val['ReceiverType'] == 2){
                    $list[$key]['ReceiverType'] = '库存补充';
                }
            }

            //关联查询数据       
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();
    }    
    
}
